package com.taomeng.servlet.admin;

import com.taomeng.entity.Department;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.RespBody;
import com.taomeng.service.DepartmentService;
import com.taomeng.servlet.BaseServlet;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@Slf4j
@WebServlet("/department/*")
public class DepartmentServlet extends BaseServlet {
    @Autowired
    private DepartmentService departmentService;

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");//第几页
        String limit = req.getParameter("limit");//每页查询几个
        String key = req.getParameter("key");       //模糊查询传过来的值
        log.info("fuzzySearchKey:" + key);

        PageBean<Department> pageBean;
        //判断
        if (null == page && null == limit) {    //添加班级时候的下拉菜单
            pageBean = departmentService.findByPage(null, null);
        } else if (null == key || "".equals(key)) {
            pageBean = departmentService.findByPage(Integer.parseInt(page), Integer.parseInt(limit));
        } else {
            pageBean = departmentService.findByPage(Integer.parseInt(page), Integer.parseInt(limit), key);
        }

        RespBody respBody = new RespBody();
        respBody.setCode(0);
        respBody.setMsg("");
        respBody.setCount(pageBean.getTotal());
        respBody.setData(pageBean.getList());

        RespBody.JsonObject(resp, respBody);

    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = req.getParameterMap();

        Department department = new Department();

        BeanUtils.populate(department, map);

        boolean flag = departmentService.addDepartment(department);

        RespBody respBody = new RespBody();

        if (flag) {
            respBody.setCode(200);
        } else {
            respBody.setCode(500);
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void delBatchAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ids = req.getParameter("ids");
        //删除成功？
        boolean flag = departmentService.delBatchAll(ids);


        RespBody respBody = new RespBody();
        if (flag) {
            respBody.setCode(200);
            respBody.setMsg("删除成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("删除失败");
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");

        boolean flag = departmentService.delDepartmentById(Integer.parseInt(id));


        RespBody respBody = new RespBody();

        if (flag) {
            respBody.setCode(200);
            respBody.setMsg("删除成功" + id);
        } else {
            respBody.setCode(500);
            respBody.setMsg("删除失败" + id);
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = req.getParameterMap();

        Department department = new Department();
        BeanUtils.populate(department, map);

        boolean flag = departmentService.updateDepartment(department);

        RespBody respBody = new RespBody();
        if (flag) {
            respBody.setCode(200);
            respBody.setMsg("更新成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("更新成功");
        }

        RespBody.JsonObject(resp, respBody);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext application = this.getServletContext();
        // 解决servlet用@Autowired自动注入service失败的问题
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, application);
    }
}
